
//
const express = require('express')
const User = require('../connect/userSchema.js')
const router = express.Router()

// 登录
router.get('/user/login',(request,response)=>{
	// console.log(request.query)
	let list = {
		phone:request.query.phone,
		psd:request.query.psd,
	}
	User.findOne({phone:list.phone},(err,data)=>{
		// console.log(data);
		if (data == null) {
			response.send({
				'status': 1004,
				'message': '用户未注册',
				'data': []
			})
		} else {
			if(list.phone == data.phone && list.psd == data.psd){
				response.status(200);
				// console.log(data.psd);
				response.send({
					'status': 1000,
					'message': "登录成功",
					"data":data
				})
			}else{
				// response.status(404);
				console.log(err);
				response.send({
					'status': 1001,
					'message': "账号或密码错误",
				})
			}
		}
	})
})

// 登录
router.get('/user/register',(req,res)=>{
	// console.log(request.query)
	User.find({phone:req.query.phone},(err,data)=>{
		// console.log(data);
		if (data == null) {
			res.send({
			    'status': 1002,
			    'message': '查询失败',
			    'data': err
			});
		} else {
			if (data.length > 0) {
			    res.send({
			        'status': 1001,
			        'message': '用户已存在',
			    })
			}else{
				User.create({
					phone:req.query.phone,
					name:req.query.name,
					psd:req.query.psd
				},(err,data)=>{
					if (err) {
					    res.send({
					        'status': 1002,
					        'message': '注册失败',
					        'data': err
					    })
					} else {
						// console.log(data)
					    res.send({
					        'status': 1000,
					        'message': '注册成功',
					    })
					}
				})
			}
		}
	})
})
// 注册


module.exports = router;